#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<syntax_analyzer.h>

///
extern int lines;
extern int pos_start;
extern int pos_end;

///
extern FILE *yyin;
extern char *yytext;
extern int yylex();
extern char token_name[20];

// Mac-only hack.
YYSTYPE yylval;

///
int main(int argc, const char **argv) {
     if (argc < 2 || argc > 3) {
          printf("usage: lexer input_file [debug]\n");
          return 0;
     }

     const char *input_file = argv[1];
     int debug = (argc == 3 && strcmp(argv[2], "debug") == 0);
     yyin = fopen(input_file, "r");
     if (!yyin) {
          fprintf(stderr, "cannot open file: %s\n", input_file);
          return 1;
     }

     int token;
     if (debug) {
          printf("%15s\t%10s\t%s\t%s\n", "Token", "Text", "Line", "Column (Start,End)");
     } else {
          printf("%15s\t%10s\t%s\n", "Token", "Text", "Line");
     }
     while ((token = yylex())) {
          if (debug) {
               printf("%15s\t%10s\t%d\t(%d,%d)\n",
                    token_name, yytext, lines, pos_start, pos_end);
          } else {
               printf("%15s\t%10s\t%d\n", token_name, yytext, lines);
          }
     }
     return 0;
}
